unit Alias;

interface

uses
  Windows, Classes, Controls, Forms, StdCtrls, Buttons, DB,
  ExtCtrls, Menus, DBCtrls, DBGrids, PortalGrid, Grids;

type
  TFormAlias = class(TForm)
    BtnDeleteAlias: TBitBtn;
    MainMenu1: TMainMenu;
    MenuHelp: TMenuItem;
    BtnAddAlias: TBitBtn;
    EditAlias: TEdit;
    ImagePointer: TImage;
    TimerPointer: TTimer;
    Panel1: TPanel;
    Status: TLabel;
    LabelLines: TLabel;
    Script: TMemo;
    Bevel1: TBevel;
    BtnUndo: TBitBtn;
    EditCopy: TEdit;
    DSAliases: TDataSource;
    GridAlias: TPortalGrid;
    procedure BtnDoneClick(Sender: TObject);
    procedure DSAliasesDataChange(Sender: TObject; Field: TField);
    procedure BtnClearClick(Sender: TObject);
    procedure GridAliasDblClick(Sender: TObject);
    procedure EditAliasKeyPress(Sender: TObject; var Key: Char);
    procedure BtnAddAliasClick(Sender: TObject);
    procedure BtnDeleteAliasClick(Sender: TObject);
    procedure GridAliasKeyPress(Sender: TObject; var Key: Char);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TimerPointerTimer(Sender: TObject);
    procedure ScriptChange(Sender: TObject);
    procedure BtnFunctionsClick(Sender: TObject);
    procedure GridAliasEnter(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnVarClick(Sender: TObject);
    procedure BtnUndoClick(Sender: TObject);
    procedure CtrlZ1Click(Sender: TObject);
    procedure BtnAMClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdatePointer;
    procedure CheckSave;
  public
    { Public declarations }
  end;

var
  FormAlias: TFormAlias;
  bypass: boolean;

implementation
uses
  SysUtils, Dialogs, DBClient, Variants,
  Database, Functions, VarGrabber, AM, refactorings, ScriptModule;

{$R *.DFM}

procedure TFormAlias.FormCreate(Sender: TObject);
begin
  bypass:=false;
end;

procedure TFormAlias.BtnDoneClick(Sender: TObject);
begin
  CheckSave;
  Datawell.lastalias:=Datawell.TableAliases.fieldbyname('Alias').asstring;
  Scripts.PrepareFunctions(datawell.TableAliases);
  close;
end;

procedure TFormAlias.DSAliasesDataChange(Sender: TObject; Field: TField);
begin
  if bypass then exit;
  Script.lines.assign(Datawell.TableAliases.fieldbyname('Script'));
  TimerPointer.enabled:=true;
  BtnUndo.enabled:=false;
end;

procedure TFormAlias.BtnClearClick(Sender: TObject);
begin
  if Script.lines.count < 1 then exit;
  Script.lines.clear;
  BtnUndo.enabled:=true;
  Script.setfocus;
end;

procedure TFormAlias.GridAliasDblClick(Sender: TObject);
begin
  Script.setfocus;
end;

procedure TFormAlias.EditAliasKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    BtnAddAlias.click;
    key:=#0;
  end;
end;

procedure TFormAlias.BtnAddAliasClick(Sender: TObject);
var
  didfind, replacing: boolean;
begin
  CheckSave;
  replacing:=false;
  if TBitBtn(sender).tag = 1 then replacing:=true;
  if (replacing) and (Datawell.TableAliases.fieldbyname('Alias').asstring = '') then begin
    messagedlg('There is no alias to replace',mtinformation,[mbok],0);
    EditAlias.setfocus;
    exit;
  end;
  if EditAlias.text = '' then begin
    messagedlg('You must enter an alias',mtinformation,[mbok],0);
    EditAlias.setfocus;
    exit;
  end;
  if Datawell.IsMacro(EditAlias.text) then begin
    messagedlg('An alias name cannot be identical to a macro',mtinformation,[mbok],0);
    EditAlias.selectall;
    EditAlias.setfocus;
    exit;
  end;
  if Datawell.IsVariable(EditAlias.text) then begin
    messagedlg('An alias name cannot be identical to a variable',mtinformation,[mbok],0);
    EditAlias.selectall;
    EditAlias.setfocus;
    exit;
  end;
  if not Datawell.ValidAlias(EditAlias.text) then begin
    messagedlg('Alias names cannot include a space $ @ " - '+
      Datawell.TableOptions.fieldbyname('Activator').asstring+' or begin with -',
      mtinformation,[mbok],0);
    EditAlias.selectall;
    EditAlias.setfocus;
    exit;
  end;
  EditAlias.text:=Datawell.CutTrailingSpaces(EditAlias.text);
  didfind:=Datawell.IsAlias(EditAlias.text);
  if didfind then replacing:=false;
  if not replacing then begin
    if didfind then begin
      messagedlg('Alias "'+EditAlias.text+'" already exists',mtinformation,[mbok],0);
      EditAlias.selectall;
      EditAlias.setfocus;
      exit;
    end;
    Datawell.TableAliases.append;
    Datawell.TableAliases.fieldbyname('Master').asinteger:=Datawell.masterinteger;
    Datawell.TableAliases.fieldbyname('Alias').asstring:=EditAlias.text;
    Datawell.TableAliases.post;
    Script.lines.clear;
  end
  else begin
    Datawell.TableAliases.edit;
    Datawell.TableAliases.fieldbyname('Alias').asstring:=EditAlias.text;
    Datawell.TableAliases.post;
  end;
  EditAlias.text:='';
  Script.setfocus;
end;

procedure TFormAlias.BtnDeleteAliasClick(Sender: TObject);
begin
  if not Datawell.TableAliases.EOF then Datawell.TableAliases.delete;
  TimerPointer.enabled:=true;
end;

procedure TFormAlias.GridAliasKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridAliasDblClick(Sender);
  end;
end;

procedure TFormAlias.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormAlias.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('aliases');
end;

procedure TFormAlias.FormShow(Sender: TObject);
var
   table: TCustomClientDataset;
begin
  table := Datawell.TableAliases;
  if not table.Locate('Master;Alias', VarArrayOf([Datawell.masterinteger, Datawell.lastalias]), []) then
    table.first;
end;

procedure TFormAlias.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridAlias) then begin
    BtnDeleteAlias.click;
    key:=0;
  end;
end;

procedure TFormAlias.ScriptChange(Sender: TObject);
begin
  LabelLines.caption:='Commands = '+inttostr(Script.lines.count);
  BtnUndo.enabled:=true;
end;

procedure TFormAlias.UpdatePointer;
begin
  DrawArrow(GridAlias, ImagePointer, 297, 6, 5);
end;

procedure TFormAlias.TimerPointerTimer(Sender: TObject);
begin
  UpdatePointer;
  TimerPointer.enabled:=false;
end;

procedure TFormAlias.BtnFunctionsClick(Sender: TObject);
var
  form: TFormFunctions;
  i, po: integer;
begin
  form:=TFormFunctions.create(self);
  form.Setup(Datawell.lastfunction,Script.seltext);
  form.showmodal;
  po:=form.po;
  form.free;
  i:=Script.selstart;
  if po < 1 then begin
    Script.setfocus;
    exit;
  end;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+po;
end;

procedure TFormAlias.GridAliasEnter(Sender: TObject);
begin
  CheckSave;
end;

procedure TFormAlias.CheckSave;
var
  i: integer;
  didfail: boolean;
begin
  didfail:=false;
  with Datawell.TableAliases do begin
    if fieldbyname('Alias').asstring <> '' then begin
      for i:=0 to Script.lines.count-1 do begin
        if Script.lines[i] = fieldbyname('Alias').asstring then begin
          didfail:=true;
          break;
        end;
      end;
      if didfail then begin
        messagedlg('An alias calling itself would cause recurstion',mtinformation,[mbok],0);
        abort;
      end;
      bypass:=true;
      edit;
      fieldbyname('Script').assign(Script.lines);
      post;
      bypass:=false;
      BtnUndo.enabled:=false;
    end;
  end;
end;

procedure TFormAlias.BtnVarClick(Sender: TObject);
var
  form: TFormVarGrab;
  grab: string;
  i, l: integer;
begin
  form:=TFormVarGrab.create(self);
  form.setup;
  form.showmodal;
  grab:=form.grab;
  form.free;
  i:=Script.selstart;
  l:=Script.sellength;
  if grab = '' then begin
    Script.setfocus;
    Script.selstart:=i;
    Script.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+length(EditCopy.text);
end;

procedure TFormAlias.BtnUndoClick(Sender: TObject);
begin
  DSAliasesDataChange(Sender,nil);
  Script.setfocus;
end;

procedure TFormAlias.CtrlZ1Click(Sender: TObject);
begin
  if BtnUndo.enabled then BtnUndo.click;
end;

procedure TFormAlias.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  grab: string;
  i, l: integer;
begin
  form:=TFormAM.create(self);
  form.avoid:=Datawell.TableAliases.fieldbyname('Alias').asstring;
  form.setup;
  form.showmodal;
  grab:=form.am;
  form.free;
  i:=Script.selstart;
  l:=Script.sellength;
  if grab = '' then begin
    Script.setfocus;
    Script.selstart:=i;
    Script.sellength:=l;
    exit;
  end;
  EditCopy.text:=grab;
  EditCopy.selectall;
  EditCopy.copytoclipboard;
  Script.pastefromclipboard;
  Script.setfocus;
  Script.sellength:=0;
  Script.selstart:=i+length(EditCopy.text);
end;

end.
